package gui;


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Set;
import java.util.Vector;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;


public class GeneralForm extends JDialog implements KeyListener, ActionListener, IObserver
{

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	protected JTable listingTable = null;

	/**
	 * @param owner
	 */
	public GeneralForm(/* Frame owner */)
	{
		super(/* owner */);

		setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setModal(true);
		initialize();
	}

	public GeneralForm(int clientID)
	{
		super(/* owner */);


		setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setModal(true);
		initialize();

	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(300, 200);
		// this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (jContentPane == null)
		{
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}

	public void keyPressed(KeyEvent e)
	{
		if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
			dispose();
	}

	public void keyReleased(KeyEvent e)
	{
		// TODO Auto-generated method stub

	}

	public void keyTyped(KeyEvent e)
	{
		// TODO Auto-generated method stub

	}

	public void actionPerformed(ActionEvent e)
	{
		if (e.getActionCommand().equalsIgnoreCase("Cancel") || e.getActionCommand().equalsIgnoreCase("ok") || e.getActionCommand().equalsIgnoreCase("close"))
		{
			dispose();
			return;
		}

		executeCommand(e.getActionCommand());

	}

	protected void executeCommand(String commandName)
	{
	}

	public void loadForm()
	{
		initComponenets();
		setVisible(true);
	}

	public void setVisible(boolean b)
	{
		setLocationRelativeTo(null);
		super.setVisible(b);
	}

	protected void initComponenets()
	{
		// TODO override this method
		initTextFields();
		initComboBoxes();

	}

	protected void initComboBoxes()
	{
		// TODO Auto-generated method stub

	}

	protected void initTextFields()
	{
		// TODO Auto-generated method stub
	}

	public void showReponse(Hashtable<String, String> response)
	{
		showReponse(this.getContentPane(), response);
	}

	public void showReponse(Container root, Hashtable<String, String> response)
	{
		Component[] components = root.getComponents();
		for (int i = 0; i < components.length; i++)
		{
			if (components[i].getClass() == JTextField.class)
			{
				((JTextField) components[i]).setText(String.valueOf(response.get(((JTextField) components[i]).getName())));
			}
			else if (components[i].getClass() == JPanel.class)
			{
				showReponse((Container) components[i], response);
			}
		}
	}	

	public void showListingData(Hashtable<String, Hashtable<String, String>> data)
	{
		// get table model
		TableModel tblModel = (TableModel) listingTable.getModel();
		// clear the table model
		tblModel.clearTableModel();

		if (data == null || data.isEmpty())
		{
			// Loggers.generalLogger.trace(
			// "Empty response vector for listing command");
			return;
		}

		String paramName = null;
		String paramValue = null;
		Object recordData = null;

//		if (tblModel.getRowCount() < data.size())
//		{
//			tblModel.setRowCount(data.size());
//		}

		final Set<String> keySet = data.keySet();
		// convert keyset to a string array so that it can be easily sorted
		String[] keys = keySet.toArray(new String[0]);
		// Arrays.sort(Object[] a) is a static sorting method
		Arrays.sort(keys);
		
		int count = 0;
		for (int i = 0; i < keys.length; i++)
		{
			recordData = data.get(keys[i]);
			if (recordData instanceof Hashtable)
			{
				tblModel.setRowCount(tblModel.getRowCount()+1);
				for (int j = 0; j < tblModel.getColumnCount(); j++)
				{
					paramName = tblModel.getColumnName(j);
					if (((Hashtable<String, Object>)recordData).containsKey(paramName))
					{
						paramValue = ((Hashtable<String, Object>)recordData).get(paramName).toString().trim();
					}
					else
					{
						paramValue = "";
					}
					tblModel.setValueAt(paramValue, count, j);
					// listingTable.setValueAt(paramValue, k, j);
				}
				count++;
			}
		}
		
	}

	public void showListingData(Vector<Hashtable<String, String>> data)
	{
		// get table model
		TableModel tblModel = (TableModel) listingTable.getModel();
		// clear the table model
		tblModel.clearTableModel();

		if (data == null || data.size() < 1)
		{
			return;
		}

		String paramName = null;
		String paramValue = null;
		Hashtable<String, String> recordData = null;

		if (tblModel.getRowCount() < data.size())
		{
			tblModel.setRowCount(data.size());
		}

		for (int i = 0; i < data.size(); i++)
		{
			recordData = data.get(i);
			for (int j = 0; j < tblModel.getColumnCount(); j++)
			{
				paramName = tblModel.getColumnName(j);
				if (recordData.containsKey(paramName))
				{
					paramValue = recordData.get(paramName).trim();
				}
				else
				{
					paramValue = "";
				}
				tblModel.setValueAt(paramValue, i, j);
				// listingTable.setValueAt(paramValue, i, j);
			}
		}
	}

	public void executeCommand(Object obj)
	{
		// TODO Auto-generated method stub
		
	}

	public void takeResult(Object result, String type)
	{
		// TODO Auto-generated method stub
		
	}

}
